--
-- DailyModel.lua
-- Created by IntelliJ IDEA.
-- User: yao.chen
-- Date: 2018/12/27
--

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ModelBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:getDailyTasks()
    local tasks = UD:getTotalDailyTasks()
    self._dailyTaskNumber = #tasks
    local totalTasks = {}
    local rewardedTasks = {}
    local finishedTasks = {}
    for _,v in ipairs(tasks) do
        v.dropItems = TaskManager:getTaskDropItems(v:getId())
        if v:isRewarded() then
            table.insert(rewardedTasks,v)
        elseif v:isFinished() then
            table.insert(finishedTasks,v)
        else
            table.insert(totalTasks,v)
        end
    end

    -- 按位置排序
    local function sortTask(a, b)
        local isSort
        local priorityA = tonumber(a.conf.priority)
        local priorityB = tonumber(b.conf.priority)

        if priorityA == priorityB then    --先根据优先级排序
            isSort = a.id < b.id                    --优先级相同，根据id排序
        else
            isSort = priorityA < priorityB
        end
        return isSort
    end

    table.sort(rewardedTasks, sortTask)
    table.sort(finishedTasks, sortTask)
    table.sort(totalTasks, sortTask)

    for i,v in ipairs(finishedTasks) do
        table.insert(totalTasks,i,v)
    end
    local tLen = #totalTasks
    for i,v in ipairs(rewardedTasks) do
        table.insert(totalTasks,tLen + i,v)
    end

    return totalTasks
end

function ClassRef:setRefreshIndex(index)
    self._refreshIndex = index
end

function ClassRef:getRefreshIndex()
    return self._refreshIndex
end

function ClassRef:getTotalDailyTaskNumber()
    return self._dailyTaskNumber or #UD:getTotalDailyTasks()
end

function ClassRef:getFinishedDailyTaskNumber()
    return #UD:getFinishedDailyTasks()
end

function ClassRef:getDailyTaskRewardProgress(needProgress)
    return TaskManager:getDailyTaskInfo():getTaskRewardProgress(needProgress)
end

function ClassRef:getDailyTaskRewards(needProgress, nowProgress)
    return TaskManager:getDailyTaskInfo():getTaskRewards(needProgress, nowProgress)
end

function ClassRef:getGetSuperPrivilegeInfo()
    app.session:request("pb_recharge.C2S_GetSuperPrivilegeInfo", {}, function(msg)
        app:sendMsg("DailyController", "updatePrivilegeInfo", msg)
    end)
end

function ClassRef:openSuperPrivilegeBox()
    app.session:request("pb_recharge.C2S_UseSuperPrivilegeBox", {}, function(msg)
        app:sendMsg("DailyController", "refreshPrivilegeBox", msg)
    end)
end

--redPoints
function ClassRef:checkRedPoints(funcId)
    if funcId == Constant.FUNC_TYPE_DAILY_TASK then     --每日任务
        return TaskManager:checkHasRewardByTaskType(2)
    elseif funcId== Constant.FUNC_TYPE_DAILY_WISH_WORLD then  -- 祝福世界
        return app:sendMsg("WorldMapModel", "getBlessTimes") > 0
    end
end

return ClassRef
